{include file="public/toper" /}
<style>
    .demo_class {width: 300px !important;}
    .demo_class .layui-layer-content {padding: 0 0 0 15px !important;}
    .laytable-cell-1-2 img {width: 50px !important;}
    .thumb-show{width: 100%;height: 100%;}
</style>
<script type="text/javascript" src="/static/js/myajax.js"></script>
<div class="x-nav">
    <span class="layui-breadcrumb">
        <a href="">标签管理</a>
        <a href="">标签列表</a>
    </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">&#xe666;</i></a>
</div>
<div class="x-body">
    <xblock class="demoTable">
        <button class="layui-btn" onclick="x_admin_show('添加标签','{:url(\'Label/add\')}','700','550')"><i class="layui-icon"></i>添加</button>
        <button class="layui-btn" id="btn-expand">全部展开</button>
        <button class="layui-btn" id="btn-fold">全部折叠</button>
    </xblock>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
<script type="text/html" id="statusTpl">
    {{#  if(d.parent_id == 0){ }}
    <input type="checkbox" name="state" value="{{ d.state }}" lay-skin="switch" lay-filter="switchStatus" lay-text="正常|关闭" {{ d.state == 1 ? 'checked' : '' }} data-href="{:url('state')}?table=label&id={{ d.label_id }}">
     {{# } }}
</script>
<script type="text/html" id="action">
    <button class="layui-btn layui-btn layui-btn-xs"  onclick="x_admin_show('编辑标签','{:url(\'Label/add\')}?id={{d.label_id}}','700','550')"><i class="layui-icon">&#xe642;</i>编辑</button>
    <button class="layui-btn-danger layui-btn layui-btn-xs del_btn" id="{{d.label_id}}" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</button>
</script>
<script type="text/html" id="image">
    {{#  if(d.image!='' && d.image!=null){ }}
    <img src="{{d.image}}" class="thumb" thumb="{{ d.image }}"/>
    {{#  } }}
</script>
<style>
    .layui-table img {height: 37px;}
</style>
<script>
    layui.config({
        base: '/static/'
    }).extend({
        treetable: 'treetable-lay/treetable'
    }).use(['layer', 'table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var layer = layui.layer;
        var treetable = layui.treetable;

        // 渲染表格
        var renderTable = function () {
            layer.load(2);
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'label_id',
                treePidName: 'parent_id',
                treeDefaultClose: true,
                treeLinkage: false,
                elem: '#test',
                url: "{url('Label/index')}",
                page: true
                ,limit: 30
                ,limits:[30,60,90,120,150,180,210,240,270,300]
                ,cols: [[
                    {type: 'numbers',width:'100',title:'序号',align: 'center'},
                    {field: 'name', title: '名称',width:'300',align: 'center'},
                    {templet: '#image', title: '图片',width:'150',align: 'center'},
                    {field: 'state', width: 140,title: '是否显示首页', templet: '#statusTpl',align: 'center'},
                    {templet: '#action', title: '操作',width:'150',align: 'center'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };

        renderTable();
        $('#btn-expand').click(function () {
            treetable.expandAll('#test');
        });


        $('#btn-fold').click(function () {
            treetable.foldAll('#test');
        });
        //删除
        table.on('tool(test)', function(obj) {
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('您确定要删除【'+data.name+'】吗？',{btnAlign: 'c'}, function(index){
                    var loading = layer.load(1, {shade: [0.1,'#fff'] });
                    $.get("{:url('Label/del')}",{id:data.label_id},function(res){
                        layer.close(loading);
                        if(res.code==200){
                            layer.msg(res.msg,{time:1000,icon:1});
                            renderTable();
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
{include file="public/footer"/}